package cn.zwxky.android.staff.Jurisdiction.Technology;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.zgscwjm.lsfbbasetemplate.Event.LsfbEvent;

import java.util.List;

import cn.zwxky.android.staff.R;
import cn.zwxky.android.staff.Utils.URLString;

/**
 * Created by Administrator on 2017/4/10 0010.
 */

public class TaskAlloAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private List<TaskAlloBean.ListBean> mList;

    public TaskAlloAdapter(Context context, List<TaskAlloBean.ListBean> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public int getGroupCount() {
        return mList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mList.get(groupPosition).getBlist().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        if (mList.size() == 0) {
            return "";
        }
        return mList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (mList.get(groupPosition).getBlist().size()==0) {
            return "";
        }
        return mList.get(groupPosition).getBlist().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        TaskAlloAdapter.GroupHolder groupHolder = null;
        if (convertView == null) {
            groupHolder = new TaskAlloAdapter.GroupHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_saddress_book_group, null);
            groupHolder.title = (TextView) convertView.findViewById(R.id.item_saddress_book_group_text_title);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (TaskAlloAdapter.GroupHolder) convertView.getTag();
        }
        groupHolder.title.setText(mList.get(groupPosition).getBname());

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        TaskAlloAdapter.GroupChild groupHolder = null;
        if (convertView == null) {
            groupHolder = new TaskAlloAdapter.GroupChild();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_task_allo, null);
            groupHolder.text_name = (TextView) convertView.findViewById(R.id.item_text_name);
            groupHolder.img_select = (ImageView) convertView.findViewById(R.id.ietm_img_select);
            groupHolder.simpleDraweeView = (SimpleDraweeView) convertView.findViewById(R.id.fg_my_img_head);
            groupHolder.tell_rl = (RelativeLayout) convertView.findViewById(R.id.item_tell_rl);

            convertView.setTag(groupHolder);
        } else {
            groupHolder = (TaskAlloAdapter.GroupChild) convertView.getTag();
        }
        groupHolder.text_name.setText(mList.get(groupPosition).getBlist().get(childPosition).getName());
        groupHolder.simpleDraweeView.setImageURI(Uri.parse(URLString.IMG_URL+mList.get(groupPosition).getBlist().get(childPosition).getImg()));
        if (mList.get(groupPosition).getBlist().get(childPosition).chack){
            groupHolder.img_select.setVisibility(View.VISIBLE);
        }else {
            groupHolder.img_select.setVisibility(View.GONE);
        }

        final RelativeLayout tell_rl = groupHolder.tell_rl;
        tell_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TaskAlloClickEvent even = new TaskAlloClickEvent();
                even.setData(mList.get(groupPosition).getBlist().get(childPosition));
                LsfbEvent.getInstantiation().post(even);
            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    private class GroupHolder {
        private TextView title;
    }

    private class GroupChild {
        private TextView text_name;
        private ImageView img_select;
        private SimpleDraweeView simpleDraweeView;
        private RelativeLayout tell_rl;
    }
}
